# -*- coding: utf-8 -*-
# File : app.py
# Author: taoyahui
# Date : 2021/7/17
import sys

sys.path.append('..')
from apscheduler.schedulers.blocking import BlockingScheduler
import configparser
import os
from utils.path_util import get_root_dir
from tasks import sys_configure, inventory_discovery
from tasks import overview,patch_regular,sync_mongo

# inventory_discovery.inventory_discovery()

cf = configparser.ConfigParser()
cf.read(os.path.join(get_root_dir(), "config.ini"))

secs = cf.sections()
host = cf.get("Mysql-Database", "host")
port = cf.get("Mysql-Database", "port")
user = cf.get("Mysql-Database", "user")
password = cf.get("Mysql-Database", "password")
db_name = cf.get("Mysql-Database", "db")

mysql_info = dict(
    host=host,
    port=port,
    dbname=db_name,
    username=user,
    password=password
)

scheduler = BlockingScheduler()


def sys_conf():
    # print("start!")
    sys_configure.get_system_configure()

#
# @scheduler.scheduled_job(id="overview_charts",
#                          trigger='interval',
#                          seconds=5)
def overview_charts():
    overview.overview_task()


def regular_patch():
    patch_regular.patch_auto_regular()

# @scheduler.scheduled_job(id="job1",
#                          args=(1,2),
#                          trigger='interval',
#                          seconds=5)
# def job1(var_one, var_two):
#     """Demo job function.
#     :param var_two:
#     :param var_two:
#     """
#     print(str(var_one) + " " + str(var_two))

def sync_mongo_patch():
    """
    定时执行任务， 将mongodb中的资产对应补丁数据更新至mysql的补丁详情表(patch)
    :return:
    """
    sync_mongo.sync_mongo_patch()


scheduler.add_job(overview_charts, 'interval', minutes=60, id="overview_charts", replace_existing=True)
# scheduler.add_job(sys_conf, 'interval', minutes=2, max_instances=1, id="sys_conf",replace_existing=True)
scheduler.add_job(regular_patch, 'interval', minutes=15, max_instances=1, id="regular_patch",replace_existing=True)
scheduler.add_job(sync_mongo_patch, 'interval', minutes=9, max_instances=1, id="sync_mongo_patch",replace_existing=True)

scheduler.start()
